﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace LittleBee.EngUnit
{
	public partial class EUValue
	{
		static Regex _reMode = new Regex(@"^(([+-]?\d+)(\.\d+)?[eE]\d{1,3}|([+-]?\d+)(\.\d+))");
		public float Value { get; set; }

		EU _eu;
		public EUValue(string strEU)
		{
			if (string.IsNullOrEmpty(strEU))
				throw new ArgumentException($"Argument can not empty.");

			Match m = _reMode.Match(strEU);
			if (m.Success)
			{
				Value = Convert.ToSingle(m.Value);
				_eu = new EU(strEU.Substring(m.Value.Length));
			}
			else
			{
				throw new ArgumentException($"Invalid argument format: {strEU}");
			}
		}

		public EUValue(EU eu)
		{
			_eu = eu;
		}

		public EUValue(float v, EU eu)
		{
			_eu = eu;
			Value = v;
		}

		public EUValue(float v, string strEU)
		{
			_eu = new EU(strEU);
			Value = v;
		}

		public static explicit operator EUValue(string strEU)
		{
			return new EUValue(strEU);
		}

		public EUValue(EUValue euv)
		{
			_eu = euv.GetEU();
			Value = euv.Value;
		}

		public EU GetEU()
		{
			return _eu;
		}

		public EUValue Exchange(string strEU)
		{
			EUValue ret = new EUValue(strEU);
			ret.Value = EU.Convert(Value, _eu, ret.GetEU());
			
			return ret;
		}

		public EUValue Exchange(EU eu)
		{
			EUValue ret = new EUValue(eu);
			ret.Value = EU.Convert(Value, _eu, eu);

			return ret;
		}

		public static EUValue Parse(string t)
		{
			throw new NotImplementedException();
		}
	}
}
